// components/code/code.ts
import api from "../../utils/api"
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    type: {
      type: String,
      value: "login"
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    imageUrl: 'data:image/png;base64,' as string,//验证码图片
    code: "" as string,//输入的值
    validateImageKey: "" as string,// imagekey
  },

  /**
   * 组件的方法列表
   */
  methods: {
    checkCode(): void {
      const { code, validateImageKey } = this.data
      this.triggerEvent("check", { code: code, validateImageKey })
    },
    handleCode(e: any): void {
      this.setData({
        code: e.detail.value
      })
    },
    closeCode(): void {
      this.triggerEvent("close")
    },
    // 获取查询他人图片验证码
    getSendReportSmsValidateImage(): void {
      api.report.getSendReportSmsValidateImage().then(res => {
        this.setData({
          imageUrl: " data:image/png;base64," + wx.arrayBufferToBase64(res.data),
          validateImageKey: res.header['x-image-key']
        })
      })
    },
    // 获取登录图片验证码
    getSmsLoginValidateImage(): void {
      api.login.getSmsLoginValidateImage().then(res => {
        this.setData({
          imageUrl: " data:image/png;base64," + wx.arrayBufferToBase64(res.data),
          validateImageKey: res.header['x-image-key']
        })
      })
    }
  },
  lifetimes: {
    attached(): void {
      if (this.data.type == "report") {
        this.getSendReportSmsValidateImage()
      } else {
        this.getSmsLoginValidateImage();
      }
    }
  }
})